import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter_map_demo/t3_map_plugin/map/core/t3_annotation.dart';
import 'package:flutter_map_demo/t3_map_plugin/map/core/t3_location.dart';


/// 原生向dart同步事件以及对应的数据

class T3MapEvent<T> {
  ///返回的内容，对应的[MethodCall]中的[[arguments]]
  final T value;

  // 构造函数
  const T3MapEvent({required this.value});
}

class T3MapRegionChangedValue {

  T3LocationCoordinate2D centerCoordinate;
  double zoomLevel;

  T3MapRegionChangedValue({required this.centerCoordinate, required this.zoomLevel});

  /// arguments数据格式
  /// ```json
  ///   {
  ///     "zoomLevel" : 17,
  ///     "coordinate" : {
  ///       "longitude": 116.359406,
  ///       "latitude": 39.910612
  ///     }
  ///   }
  /// ```
  static T3MapRegionChangedValue? fromString(dynamic arguments) {
    if (arguments is String) {
      try {
        final json = jsonDecode(arguments);
        final zoomLevel = json['zoomLevel'];
        final coordinateValue = json['coordinate'];
        final longitude = coordinateValue['longitude'];
        final latitude = coordinateValue['latitude'];
        if (zoomLevel is num && longitude is num && latitude is num) {
          return T3MapRegionChangedValue(centerCoordinate: T3LocationCoordinate2D(
                                                                                  longitude: longitude.toDouble(), 
                                                                                  latitude: latitude.toDouble()
                                                                                ), 
                                        zoomLevel: zoomLevel.toDouble());
        }
        else {
          debugPrint("T3MapRegionChangeValue double数据类型转换失败");
          return null;
        }
      } catch (e) {
        debugPrint("T3MapRegionChangeValue 数据解析失败，${e.toString()}");
        return null;
      }
    }
    else {
      return null;
    }
  }
}

/// 操作事件回调值
class T3MapActionEventValue {
  final bool wasUserAction;
  final bool animated;
  const T3MapActionEventValue({required this.animated, required this.wasUserAction});

  factory T3MapActionEventValue.fromString(dynamic arguments) {
    if (arguments is String) {
      try {
        final json = jsonDecode(arguments);
        final wasUserActionValue = json['wasUserAction'];
        final animatedValue = json['animated'];
        
        final bool wasUserAction;
        final bool animated;

        if (wasUserActionValue is bool) {
          wasUserAction = wasUserActionValue;
        } else {
          wasUserAction = false;
        }

        if (animatedValue is bool) {
          animated = animatedValue;
        } else {
          animated = false;
        }
        return T3MapActionEventValue(animated: animated, wasUserAction: wasUserAction);
      } catch (e) {
        return const T3MapActionEventValue(animated: false, wasUserAction: false);
      }
    } else {
      return const T3MapActionEventValue(animated: false, wasUserAction: false);
    }
  }

  @override
  String toString() {
    return "wasUserAction: $wasUserAction, animated: $animated";
  }
}

/// 地图加载成功
class T3MapDidFinishLoadingEvent extends T3MapEvent<bool> {
  const T3MapDidFinishLoadingEvent({required super.value});
}

/// 地图加载完成，（在此之后，可以进行坐标计算）
class T3MapCompleteEvent extends T3MapEvent<bool> {
  const T3MapCompleteEvent({required super.value});
}

/// 实时位置信息，来源于地图
class T3LocationChangedEvent extends T3MapEvent<T3MapLocation> {
  const T3LocationChangedEvent({required super.value});
}

/// 地图区域即将改变时会调用此接口
class T3MapRegionWillChangeEvent extends T3MapEvent<T3MapActionEventValue> {
  const T3MapRegionWillChangeEvent({required super.value});
}

/// 地图区域改变过程中会调用此接口
class T3MapRegionChangedEvent extends T3MapEvent<T3MapRegionChangedValue> {
  const T3MapRegionChangedEvent({required super.value});
}

/// 地图区域改变完成后会调用此接口
class T3MapRegionDidChangeEvent extends T3MapEvent<T3MapActionEventValue> {
  const T3MapRegionDidChangeEvent({required super.value});
}

/// [Polyline] 的点击事件
class T3MapDidTapPolylineEvent extends T3MapEvent<String> {
  const T3MapDidTapPolylineEvent({required super.value});
}

/// [Annotation] 的点击事件
class T3MapDidTapAnnotationEvent extends T3MapEvent<T3Annotation> {
  const T3MapDidTapAnnotationEvent({required super.value});
}

/// 用户拖动地图事件
class T3MapTouchEvent extends T3MapEvent<bool> {
  T3MapTouchEvent({required super.value});
}